package com.example.treasure_back.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.treasure_back.entity.MsgEntity;
import com.example.treasure_back.mapper.ThumbMapper;
import com.example.treasure_back.model.ThumbModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ThumbService {
    @Autowired
    private ThumbMapper thumbMapper;
    @Autowired
    private EssayService essayService;

    public ThumbModel getInfoById(Integer essayId, String userId) {
        QueryWrapper<ThumbModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("essay_id", essayId).eq("user_id", userId);
        ThumbModel thumbModel = thumbMapper.selectOne(queryWrapper);
        if (thumbModel == null) {
            return new ThumbModel(0, essayId, userId, 0, 0);
        }
        return thumbModel;
    }

    public Boolean setLikeById(Integer essayId, String userId) {
        QueryWrapper<ThumbModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("essay_id", essayId).eq("user_id", userId);
        ThumbModel thumbModel = thumbMapper.selectOne(queryWrapper);
        // 没用找寻到字段，创建新的
        if (thumbModel == null) {
            ThumbModel model = new ThumbModel();
            model.setEssayId(essayId);
            model.setUserId(userId);
            model.setIsDislike(0);
            model.setIsLike(1);
            thumbMapper.insert(model);
            essayService.addNum(true,essayId);
            return true;
        }
        // 找到字段，直接修改
        UpdateWrapper<ThumbModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", thumbModel.getId());
        if(thumbModel.getIsLike() == 1){
            updateWrapper.set("is_like", 0);
            essayService.reduceNum(true,essayId);
        }
        else{
            updateWrapper.set("is_like", 1);
            essayService.addNum(true,essayId);
        }
        thumbMapper.update(thumbModel, updateWrapper);
        return true;
    }

    public Boolean setDisLikeById(Integer essayId, String userId) {
        QueryWrapper<ThumbModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("essay_id", essayId).eq("user_id", userId);
        ThumbModel thumbModel = thumbMapper.selectOne(queryWrapper);
        if (thumbModel == null) {
            ThumbModel model = new ThumbModel();
            model.setEssayId(essayId);
            model.setUserId(userId);
            model.setIsDislike(1);
            model.setIsLike(0);
            thumbMapper.insert(model);
            essayService.addNum(false,essayId);
            return true;
        }
        UpdateWrapper<ThumbModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", thumbModel.getId());
        if(thumbModel.getIsDislike() == 1){
            updateWrapper.set("is_dislike", 0);
            essayService.reduceNum(false,essayId);
        }
        else{
            updateWrapper.set("is_dislike", 1);
            essayService.addNum(false,essayId);
        }
        thumbMapper.update(thumbModel, updateWrapper);
        return true;
    }
}
